package com.yanqu.road.server.cmd.marriage;

import com.yanqu.road.entity.player.UserMarriageData;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.MarriagePb;
import com.yanqu.road.entity.marriage.MarriageInfo;
import com.yanqu.road.pb.marriage.MarriageProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.MarriageModule;
import com.yanqu.road.server.manger.CrossUnionServerGroupMgr;
import com.yanqu.road.server.manger.marriage.MarriageMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.utils.ConfigHelper;

import java.util.List;

@Cmd(code = Protocol.S_MARRIAGE_GET_SERVER_APPLY_LIST, desc = "获取全服联姻请求列表")
public class MarriageGetServerApplyCmd extends AbstractCommand {

    private int childSex;
    private int type;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        MarriageProto.MarriageGetServerApplyReqMsg reqMsg = MarriageProto.MarriageGetServerApplyReqMsg.parseFrom(packet.getBytes());
        childSex = reqMsg.getChildSex();
        type = reqMsg.getApplyType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(type == 1){
            MarriageMgr.getUnionApplyList(player, childSex);
        }else {

            //前n次联姻，强制npc,即本服联姻
            UserMarriageData userMarriageData = player.getModule(MarriageModule.class).getUserMarriageData();
            long marriageTimes = 0;
            if(null != userMarriageData) {
                marriageTimes = userMarriageData.getTotalMarriageTimes();
            }

            //本服列表
            //是否开启商会分组
            long serverId = ConfigHelper.getLong("serverId");
            if(CrossUnionServerGroupMgr.getGroupServerCountByServerId(serverId) > 1 && marriageTimes >= GameConfig.MARRIAGE_CAN_NPC_TIMES){
                MarriageProto.MarriageGetServerApplyReqMsg.Builder reqMsg = MarriageProto.MarriageGetServerApplyReqMsg.newBuilder();
                reqMsg.setApplyType(type);
                reqMsg.setChildSex(childSex);
                player.sendPacket(Protocol.C_CROSS_WORLD_MARRIAGE_GET_APPLY_LIST, reqMsg);
                return;
            }
            List<MarriageInfo> marriageInfoList = MarriageMgr.getServerApplyList(player, childSex);
            MarriageProto.MarriageApplyListMsg.Builder applyListData = MarriagePb.parseMarriageApplyListMsg(marriageInfoList);
            player.sendPacket(Protocol.U_MARRIAGE_GET_SERVER_APPLY_LIST, applyListData);
        }
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
